<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :form="true"
    :destroy-on-close="true"
    :width="750"
    v-model="visible"
    :title="isUpdate ? '修改报表类型' : '添加报表类型'"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.prevent=""
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型名称" prop="title">
            <el-input
              clearable
              v-model="form.title"
              placeholder="请填写类型名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">

          <el-form-item label="所属类型" prop="parentId">
            <ele-tree-select
              clearable
              placeholder="请选择所属类型"
              v-model="form.parentId"
              :popper-width="240"
              :tree-props="treeProps"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="显示顺序" prop="orderNum">
            <el-input
              clearable
              v-model="form.orderNum"
              placeholder="请填写显示顺序"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              :rows="4"
              type="textarea"
              v-model="form.remark"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import {ref, reactive, watch} from 'vue';
import {EleMessage} from 'ele-admin-plus';
import {useFormData} from '@/utils/use-form-data';
import {addType, updateType, listTypes} from '@/api/manage/jimu/reportType';
import {handleTree} from '@/utils/linyang.js';

const props = defineProps({
  /** 修改回显的数据 */
  data: Object
});

const emit = defineEmits(['done']);

/** 弹窗是否打开 */
const visible = defineModel({type: Boolean});

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);


/** 树属性 */
const treeProps = reactive({
  //height: 266,
  data: [],
  expandOnClickNode: false
});

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  id: void 0,
  title: '',
  parentId: void 0,
  orderNum: void 0,
  remark: ''
});

/** 表单验证规则 */
const rules = reactive({});

/** 关闭弹窗 */
const handleCancel = () => {
  visible.value = false;
};

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateType : addType;
    saveOrUpdate(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success({message: msg, plain: true});
        handleCancel();
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error({message: e.message, plain: true});
      });
  });
};

/** 监听弹窗打开 */
watch(visible, () => {
  if (visible.value) {
    selectTypes();
    if (props.data) {
      assignFields(props.data);
      isUpdate.value = true;
    } else {
      resetFields();
      isUpdate.value = false;
    }
  }
});

const selectTypes = () => {
  listTypes().then(res => {
    if (res) {
      res.forEach(item => {
        item.label = item.title;
      });
      treeProps.data = handleTree(res);
    }
  });
}


</script>
